package com.qin.controller;

import com.alibaba.fastjson.JSONObject;
import com.qin.entity.ExecuteBean;
import com.qin.entity.Function;
import com.qin.entity.TestCase;
import com.qin.enums.RetCode;
import com.qin.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("functionController")
@RequestMapping("/function")
@ResponseBody
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    @RequestMapping(value="/queryFunctions", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryFunctions(@RequestBody String body){
//        System.out.println("web层：queryFunctions");
        System.out.println("接口调用：/function/queryFunctions");

        //入参封装到实体类
        Function function = JSONObject.parseObject(body, Function.class);
        System.out.println("接口入参：" + body);
        //调用service层
        String retString = functionService.queryFunctions(function).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/insertFunction", produces="text/html;charset=UTF-8")
    public String insertFunction(@RequestBody String body){
//        System.out.println("web层：insertFunction");
        System.out.println("接口调用：/function/insertFunction");

        //入参封装到实体类
        Function function = JSONObject.parseObject(body, Function.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((function.getFunction_name() == null) || ("".equals(function.getFunction_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((function.getSystem_name() == null) || ("".equals(function.getSystem_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "system_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = functionService.insertFunction(function).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/deleteFunction", produces="text/html;charset=UTF-8")
    public String deleteFunction(@RequestBody String body){
//        System.out.println("web层：deleteFunction");
        System.out.println("接口调用：/function/deleteFunction");

        //入参封装到实体类
        Function function = JSONObject.parseObject(body, Function.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((function.getFunction_id() == null) || ("".equals(function.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = functionService.deleteFunction(function).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/updateFunction", produces="text/html;charset=UTF-8")
    public String updateFunction(@RequestBody String body){
//        System.out.println("web层：updateFunction");
        System.out.println("接口调用：/function/updateFunction");

        //入参封装到实体类
        Function function = JSONObject.parseObject(body, Function.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((function.getFunction_id() == null) || ("".equals(function.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = functionService.updateFunction(function).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

}
